package cn.e3.content.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.e3.content.service.ContentService;
import cn.e3.jedis.JedisClient;
import cn.e3.mapper.TbContentMapper;
import cn.e3.pojo.TbContent;
import cn.e3.pojo.TbContentExample;
import cn.e3.pojo.TbContentExample.Criteria;
import cn.e3.utils.JsonUtils;
@Service
public class ContentServiceImpl implements ContentService{
	@Autowired
	private TbContentMapper tbContentMapper;
	@Autowired
	private JedisClient jedisClient;
	@Value("$(Content_list)")
	private String 	Content_list;
	/*
	 * 查询轮播图照片
	 */
	public List<TbContent> getContentByCid(Long cid) {
		//查看redis有没有缓存
		try {
			String hget = jedisClient.hget(Content_list, cid+"");
			if(StringUtils.isNotBlank(hget)){
				List<TbContent> list = JsonUtils.jsonToList(hget,TbContent.class);
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		TbContentExample example=new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(cid);
		List<TbContent> list = tbContentMapper.selectByExampleWithBLOBs(example);
		//将查询到的数据放入redis缓存
		try {
			jedisClient.hset(Content_list, cid+"", JsonUtils.objectToJson(list));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

}
